package org.ans.jpodcast.responsehandler;

import java.io.IOException;
import java.util.ArrayList;
import org.ans.jpodcast.client.PcPProperties;
import org.ans.jpodcast.constants.PcPXmlKeys;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;

/**
 * This class is an implementation of HttpClient's ResponseHandler interface. It will return a PcPProperties
 * object when handed an HttpResponse from the PCP "info" web service.
 * The info web service is found at
 * @author andy
 */
public class InfoResponseHandler extends GenericResponseHandler {

    private String stringTest;

    @Override
    public PcPProperties handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

        // Call the superclass method to set up the XML document on the class
        super.handleResponse(response);

        // Build the PcPProperties object
        PcPProperties properties = new PcPProperties();

        // Set the server version (1.0 for 10.5 Podcast Producer, 2.0 for 10.6 Podcast Producer)
        Float serverVersion = Float.valueOf(getSimpleProperty(PcPXmlKeys.SERVER_VERSION));
        properties.setServerVersion(serverVersion);

        // Set the server's unique identifier
        properties.setServerUUID(getSimpleProperty(PcPXmlKeys.SERVER_UUID));

        // Report what HTTP authentication methods the server supports
        // For Kerberos, also retrieve the krb principals
        ArrayList authOptions = getArrayProperty(PcPXmlKeys.HTTP_AUTH_OPTIONS);
        if (authOptions.contains("basic")) {
            properties.setSupportsBasicAuth(true);
        }
        if (authOptions.contains("digest")) {
            properties.setSupportsDigestAuth(true);
        }
        if (authOptions.contains("kerberos")) {
            properties.setSupportsKerberosAuth(true);
            properties.setKrbServicePrincipals(getArrayProperty(PcPXmlKeys.KERBEROS_PRINCIPALS));
        }

        return properties;

    }

    public String getTestString() {
        return stringTest;
    }
}
